<!DOCTYPE html>
            
<HTML>
<HEAD>
<meta name="booktitle" content="Developing Applications With Objective Caml" >
 <meta charset="ISO-8859-1"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<META name="GENERATOR" content="hevea 1.05-7 of 2000-02-24">
<META NAME="Author" CONTENT="Christian.Queinnec@lip6.fr">
<LINK rel=stylesheet type="text/css" href="videoc-ocda.css">
<script language="JavaScript" src="videoc.js"><!--
//--></script>
<TITLE>
 Portability and Efficiency
</TITLE>
</HEAD>
<BODY class="regularBody">
<A HREF="book-ora067.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora069.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2> Portability and Efficiency</H2>
<A NAME="@concepts144"></A>
<A NAME="@concepts145"></A>
One reason to compile to an abstract machine is to produce an
executable independent of the architecture of the real machine where it runs. 
A native compiler will produce more efficient code, but the binary can only be executed on the architecture it was compiled for.<BR>
<BR>
<A NAME="toc99"></A>
<H3> Standalone Files and Portability</H3>
To produce a standalone executable, the bytecode compiler links 
the bytecode object file <TT>example.cmo</TT> with the runtime library, the bytecode interpreter and some C code. It is assumed that there is a C compiler
on the host system. The inclusion of machine code means that stand-alone bytecode executables are not portable to other systems or other architectures.<BR>
<BR>
This is not the case for the non-standalone version. Since the Zinc machine is not included, the only
things generated are the platform independent
bytecode instructions. Bytecode programs will run on any platform that has the interpreter. <TT>Ocamlrun</TT> is part of the default Objective CAML distribution for Sparc running Solaris, Intel running Windows, etc. It is always preferable to use the same version of interpreter and compiler.<BR>
<BR>
The portability of bytecode object files makes it possible to directly
distribute Objective CAML libraries in bytecode form.<BR>
<BR>
<A NAME="toc100"></A>
<H3> Efficiency of Execution</H3>The bytecode compiler produces a sequence of instructions for the Zinc machine,
which at the moment of the execution, will be interpreted by <TT>ocamlrun</TT>. Interpretation has a moderately negative
linear effect on speed of execution. It is possible to view Zinc's bytecode interpretation as a big pattern matching machine (matching <TT>match</TT> ... <TT>with</TT>) where each instruction is a trigger and the computation branch modifies the stack and the counter (address of the next instruction).<BR>
<BR>
Without testing all parts of the language, the following small example
which computes Fibonacci numbers shows the difference in execution time between the bytecode compiler and the native compiler. Let the program <TT>fib.ml</TT> as follows:
<PRE>
let rec fib n = 
  if n &lt; 2 then 1
  else (fib (n-1)) + (fib(n-2));;
</PRE>and the following program <TT>main.ml</TT> as follows:
<PRE>
for i = 1 to 10 do 
  print_int (Fib.fib 30);
  print_newline()
done;;
</PRE>Their compilation is as follows:
<PRE>
$ ocamlc -o fib.exe fib.ml main.ml
$ ocamlopt -o fibopt.exe fib.ml main.ml
</PRE>These commands produce two executables: <TT>fib.exe</TT> and <TT>fibopt.exe</TT>. Using the Unix command time in Pentium 350 under Linux, we get the following data:
<DIV ALIGN=center>
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1>
<TR><TD  ALIGN=left NOWRAP><TT>fib.exe</TT> (bytecode)</TD>
<TD  ALIGN=left NOWRAP><TT>fibopt.exe</TT> (native)</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>7 s</TD>
<TD  ALIGN=left NOWRAP>1 s</TD>
</TR></TABLE>
</DIV>
This corresponds to a factor 7 between the two versions of the same program. 
This program does not test all characteristics of the language. The difference depends
heavily on the type of application, and is typically much smaller.<BR>
<BR>
<BR>
<BR>

<BR>
<BR>
<HR>
<A HREF="book-ora067.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora069.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
